﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JoyeriaProject
{
    public partial class frmBuy : Form
    {
        public int CustomerID;
        
        private ModeloDatos MD = new ModeloDatos();
       
        public frmBuy()
        {
            InitializeComponent();
        }

        private void frmBuy_Load(object sender, EventArgs e)
        {
            CustomSQL t = new CustomSQL
            {
                Insert = false,
                Update = false,
                Select = true,
                Table = "Customer",
                Where = true,
                OrderBy = false,
                KeyName = "CustomerID",
                KeyValue = CustomerID,  
            };

            t.Columns.Add(new SimpleDatabaseValues { parameter = "Articulos", value = "" });

       //     string[] Articulos = (string[])MD.CustomSQLTest(t) ;
        //    dateTimePick.Value = DateTime.Now;

         //   txtArticulo.Text = (Articulos.Count() > 0 ? Articulos[0] : "");

            foreach (Agentes agente in MD.GetAgents())
                comboAgente.Items.Add(agente.Nombre + " " + agente.Apellidos);
      
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            List<SimpleDatabaseValues> Values = new List<SimpleDatabaseValues>();
           
            double Num = 0;
            if (txtAmount.Text != string.Empty && double.TryParse(txtAmount.Text, out Num))
            {
                double saldo = 0;

                foreach (var Element in MD.GetComprasPagos().GroupBy(n => n.CustomerID).Where(ff => ff.Key == CustomerID).Select(g => new { Result = g.Sum(p => p.Cargo) - g.Sum(p => p.Abono) }))
                    saldo = Element.Result - Convert.ToDouble(txtAmount.Text);

                MD.PaymentShopping = new ComprasPagos
                {
                    ID = System.Guid.NewGuid().ToString(),
                    CustomerID = CustomerID,
                    Fecha = dateTimePick.Value.Month.ToString("00") + dateTimePick.Value.Day.ToString("00") + dateTimePick.Value.Year.ToString("0000"),
                    Date = dateTimePick.Value,
                    Cargo = Convert.ToDouble(txtAmount.Text),
                    Abono = 0,
                    Saldo = saldo,
                    Articulo = txtArticulo.Text,
                    Firma = comboAgente.SelectedItem.ToString(),
                };

                if (MD.Add<ComprasPagos>(ref MD.PaymentShopping))
                {
                    //CustomSQL t = new CustomSQL
                    //{
                    //    Insert = false,
                    //    OrderBy = false,
                    //    Select = false,
                    //    Table = "Customer",
                    //    Where = true,
                    //    Update = true,
                    //    KeyName = "CustomerID",
                    //    KeyValue = CustomerID,
                    //};

               //     t.Columns.Add(new SimpleDatabaseValues { parameter = "Articulos", value = txtArticulo.Text + " (" + dateTimePick.Value.ToString("dd/MM/yyyy") + ")" });

                //    if (!(bool)(MD.CustomSQLTest(t)))
                //        MessageBox.Show("Hubo un problema al actualizar los articulos comprados por el cliente", ":(  HUBO UN PROBLEMA", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    MessageBox.Show("La Compra se Registro Satisfactoriamente", "LISTO!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Dispose();
                }
                else
                {
                    MessageBox.Show("Hubo un problema al grabar el registro de la Compra ", ":(  HUBO UN PROBLEMA", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
              
            }
            else
            {
                MessageBox.Show("Coloca o Corrije el Monto a pagar", "ESPERA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }
        
    }
}
